<?php

declare(strict_types=1);

namespace app\api\controller\v1;


use think\facade\Db;
use think\Request;

class Order
{
    /**
     * 根据订单号和交易号搜索符合条件的订单记录
     *
     * @param Request $request - 请求对象，包含查询订单号和查询交易号
     * @return \think\Response
     */
    public function search(Request $request)
    {
        $orderNo = $request->param('ordernum', '');
        $transactionNo = $request->param('transactionNo', '');

        if ($orderNo === '' && $transactionNo === '') {
            return json([
                'code' => 400,
                'msg' => '请提供正确的查询条件',
                'data' => null
            ]);
        }

        $query = Db::name('pc_mall_order_form');
        if ($orderNo !== '') {
            $query->field('ordernum')->where('ordernum', 'like', "%$orderNo%");
        }

        if ($transactionNo !== '') {
            $query->field('transactionNo')->where('transactionNo', 'like', "%$transactionNo%");
        }

        $orders = $query->select();

        return json([
            'code' => 200,
            'msg' => '查询成功',
            'data' => [
                'orders' => $orders,
            ]
        ]);
    }
}
